using System.Runtime.InteropServices;

namespace WindowsAPI.PE
{
    [StructLayout(LayoutKind.Explicit)]
    public struct IMAGE_NT_HEADERS32
    {
        [FieldOffset(0)]
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public char[] Signature;

        [FieldOffset(4)]
        public File_Header FileHeader;

        [FieldOffset(24)]
        public Optional_Header32 OptionalHeader;

        private string _Signature { get { return new string(Signature); } }

        public bool isValid
        {
            get
            {
                return _Signature == "PE\0\0" 
                    && (OptionalHeader.Magic == MagicType.IMAGE_NT_OPTIONAL_HDR32_MAGIC 
                        || OptionalHeader.Magic == MagicType.IMAGE_NT_OPTIONAL_HDR64_MAGIC);
            }
        }
    }

    [StructLayout(LayoutKind.Explicit)]
    public struct IMAGE_NT_HEADERS64
    {
        [FieldOffset(0)]
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public char[] Signature;

        [FieldOffset(4)]
        public File_Header FileHeader;

        [FieldOffset(24)]
        public Optional_Header64 OptionalHeader;

        private string _Signature
        {
            get { return new string(Signature); }
        }

        public bool isValid
        {
            get { return _Signature == "PE\0\0" && (OptionalHeader.Magic == PE.MagicType.IMAGE_NT_OPTIONAL_HDR32_MAGIC || OptionalHeader.Magic == PE.MagicType.IMAGE_NT_OPTIONAL_HDR64_MAGIC); }
        }
    }
}